package com.xiaoxiang.pay.controller;

import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.pay.enumeration.AliPayTradeStatusEnum;
import com.xiaoxiang.pay.param.CreateOrderParam;
import com.xiaoxiang.pay.service.PayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: haha
 * @date: 2022/4/2 11:07
 */
@SuppressWarnings("JavadocReference")
@RestController
@RequestMapping("alipay")
public class AliPayController {

    @Resource(name = "alipayService")
    private PayService alipayService;

    @Value("${alipay.charset}")
    private String charset;

    @PostMapping("order")
    public HttpResult createOrder(CreateOrderParam param) {
        return HttpResult.success(alipayService.createOrder(param));
    }

    /**
     * 支付宝异步回调
     * @see https://opendocs.alipay.com/open/203/105286
     */
    @PostMapping("notify")
    public HttpResult notify(@RequestParam("out_trade_no") Long orderId,
                             @RequestParam("trade_status") AliPayTradeStatusEnum statusEnum) {
        alipayService.resultCallBack(orderId, statusEnum.getPayStateEnum());
        return HttpResult.success();
    }

}
